package com.shoppingwebsite.controller;

import com.shoppingwebsite.domain.*;
import com.shoppingwebsite.service.OrderService;
import com.shoppingwebsite.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.UnsupportedEncodingException;
import java.util.List;

@RestController
@RequestMapping("/order")
public class OrderController {
    @Autowired
    private OrderService orderService;
    @Autowired
    private UserService userService;

    //得到当前用户
    @GetMapping("/getNow")
    public Result getNow(){
        Integer userId = BaseContext.getCurrentId();
        YtUser user = userService.selectById(userId);
        //返回结果
        Result result=new Result();
        result.setFlag(true);
        result.setInfo(user.getUsername());
        return result;
    }

    //得到当前用户订单信息
    @GetMapping("/getNowTable")
    public List<YtOrders> getNowTable(String username){
        //避免中文乱码
        try {
            username = new String(username.getBytes("iso8859-1"),"utf-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        //根据用户名得到所有订单
        List<YtOrders> ordersTable = orderService.getOrders(username);
        return ordersTable;
    }
}
